package ChatSystem.ClientDemo.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 主窗口，发送消息，接收消息
 * @create @create 2022.5.29
 * @author Administrator
 *
 */
public class ClientMainFrame extends JFrame implements ActionListener{

	//端口号
	private int post;
	
	//客户端登录名称
	private String name;
	
	//Socket
	private Socket socket;
	
	//发送消息按钮
	private JButton send = new JButton("发送");
	
	//清空消息
	private JButton clear = new JButton("清空");
	
	//历史消息列表
	private JTextArea last = new JTextArea();
	private JScrollPane lastPanel = new JScrollPane(last);
	
	//发送消息框
	private JTextArea now = new JTextArea();
	private JScrollPane nowPanel = new JScrollPane(now);
	
	public ClientMainFrame(int post, String name){
		this.post = post;
		this.name = name;
		
		try {
			socket = new Socket(InetAddress.getLocalHost(),post);
			
		} catch (UnknownHostException e) {
			JOptionPane.showMessageDialog(null,"未查询到该端口号或ip地址");
			this.dispose();
			new ClientSetPost();
			return;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//设置自动换行
		now.setLineWrap(true);
		last.setLineWrap(true);
		
		//设置历史信息不可更改
		last.setEditable(false);
		
		//接收信息内部类
		ReceiveNews receive = new ReceiveNews();
		receive.start();
		
		//初始化
		init();
	}
	
	private void init(){
		
		//设置窗体名称
		this.setTitle("客户端");
		
		//设置窗体大小不可改
		this.setResizable(false);
		
		//设置窗口关闭方式
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置字体
		send.setFont(new Font("宋体",1,18));
		clear.setFont(new Font("宋体",1,18));
		
		//添加监听
		send.addActionListener(this);
		clear.addActionListener(this);
		
		//设置布局
		this.setLayout(null);
		this.setBounds(700,200,600,600);
		clear.setBounds(500,400,90,60);
		send.setBounds(500,500,90,60);
		nowPanel.setBounds(10,400,480,160);
		lastPanel.setBounds(10,10,570,380);
		
		//添加组件
		this.add(send);
		this.add(clear);
		this.add(nowPanel);
		this.add(lastPanel);
		
		//设置窗体可见
		this.setVisible(true);
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		JButton button = (JButton)e.getSource();
		if(button.equals(clear)){//清空
			now.setText("");
		}else if(button.equals(send)){//发送
			if(!"".equals(now.getText())){
				new SendNews(now.getText()).start();
				now.setText("");
			}
		}
		
	}
	
	//内部类，用与接收信息
	class ReceiveNews extends Thread{
		
		//声明输入流
		private DataInputStream in;
		
		public ReceiveNews(){
			try {
				//创建输入流对象
				in = new DataInputStream(socket.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void run(){
			
			while(true){
				
				String temp = "";
				
				try {
					temp = in.readUTF();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				last.setText(last.getText() + temp + "\n");
				
			}
			
		}
		
	}
	
	//发送信息内部类
	class SendNews extends Thread{
		
		private String message;
		
		public DataOutputStream out = null;
		
		public SendNews(String message){
			try {
				out = new DataOutputStream(socket.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			this.message = message;
		}
		
		@Override
		public void run(){
			
			try {
				out.writeUTF(name + "：" + message);
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}

}
